<?php
namespace Common\Logic;

/**
 * 房间逻辑
 * @modify trainyao 2016-01-05 创建
 */
class SaveHotelLogic extends \Common\Controller\BaseController {
	/**
	 * 开启事务，同时更新多条房型特色信息 与设置房型价格，押金
	 * @modify wenhui 2016-11-11	创建
     * @modify wenhui 2016-12-22    修改 保存房型价格在微预定价格日历表bk_hotel_room_type_price
     * @param  integer   $hotelId    客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  decimal   $price       价格
     * @param  string   $description 房型上架描述
     * @param  string   $coverImageUrl  封面图片url
     * @param  decimal   $deposit  订单押金
     */
    public function saveInfo($hotelId, $room, $deposit = null) {
    	$roomTypeDetailModel = D('RoomTypeDetail');
        $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
        $hotelRoomTypePriceModel->startTrans();
        $roomTypeDetailModel->startTrans();
        $commit = true;

        if (!is_array($room)) {
        	returnJson(500, 506);
            return;
        }

        $roomTypeDetail = $roomTypeDetailModel->getInfoByHotel($hotelId);
        $roomTypeDetail =getArrayValueAsIndex($roomTypeDetail, 'roomtype_id');
        $hotelPayDetailModel = D('HotelPayDetail');

        // 保存基本信息
        foreach ($room as $roomItem) {
            $roomTypeId = $roomItem['roomType'];
            $description = $roomItem['description'];
            $price = $roomItem['price'];

            // 先查询是否已经插入数据,不存在先新建数据        
            if (!isset($roomTypeDetail[$roomTypeId])) {
                $saveResult = $roomTypeDetailModel->addInfo($hotelId, $roomTypeId, DISABLE);
                if (empty($saveResult)) {
                    $commit = false;
                }
            }
            if (is_null($deposit)) {
                // 保存型特色信息
                $saveResult = $roomTypeDetailModel->saveInfo($hotelId, $roomTypeId, null, $description, null);
                if (empty($saveResult)) {
                    $commit = false;
                }
            } else {
                //$saveResult = $roomTypeDetailModel->saveInfo($hotelId, $roomTypeId, $price, null, null); 原来保存的地方
                // 保存房型价格
                $currentData = strtotime(date('Y-m-d'));
                $hotelRoomTypePriceInfo = $hotelRoomTypePriceModel->getInfoByRoomType($roomTypeId, $currentData);
                $saveResult = $hotelRoomTypePriceModel->setPrice($roomTypeId, $currentData, $price);
                // 获取客栈默认符号
                $symbol = $this->getDefultCurrencyByHotel($hotelId);
                if($hotelRoomTypePriceInfo){
                    if($hotelRoomTypePriceInfo['price'] != $price){
                        $detail = '价格由'.$symbol.$hotelRoomTypePriceInfo['price'].'改为'.$symbol.number_format($price, 2, '.', '');

                        priceChangeLog($hotelId, $roomTypeId, $detail, $currentData, '', DISTR_PRICE, '');
                    }
                }else{
                    $detail = '价格设置为'.$symbol.number_format($price, 2, '.', '');
                    priceChangeLog($hotelId, $roomTypeId, $detail, $currentData, '', DISTR_PRICE, '');
                }
                if (empty($saveResult)) {
                    $commit = false;
                }

                // 保存订单押金
                $saveResult = $hotelPayDetailModel->saveInfo($hotelId, null, null, null, null, null, null, null, $deposit);
                if (empty($saveResult)) {
                    $commit = false;
                }
            }
        }
        unset($roomItem);

        if ($commit) {
            $roomTypeDetailModel->commit();
            $hotelRoomTypePriceModel->commit();
            return true;
        } else {
            $roomTypeDetailModel->rollback();
            $hotelRoomTypePriceModel->rollback();
            returnJson(500, 506);
            return false;
        }
    }

    /**
     * 客栈默认货币
     * @modify wenhui 2018-02-05    创建
     */
    protected function getDefultCurrencyByHotel($hotel) {
        $currencyModel = D('Currency');
        $currencyHotelModel = D('CurrencyHotel');

        $bpCurrency = $currencyModel->getCurrencyInfo();
        $bpCurrency = getArrayValueAsIndex($bpCurrency, 'id');
        $defaultHotelCurrency = $currencyHotelModel->getDefaultCurrency($hotel);
        $defaultSymbol = isset($bpCurrency[$defaultHotelCurrency['currency_id']]['symbol']) ? $bpCurrency[$defaultHotelCurrency['currency_id']]['symbol'] : '¥';

        return $defaultSymbol;
    }

}